package com.mobisystems.youpark;

import com.parse.ParseException;
import com.parse.ParseUser;
import com.parse.SignUpCallback;
import com.mobisystems.youpark.utils.UtilityFunctions;

import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterFragment extends Fragment implements View.OnClickListener {
	private EditText etName;
	private EditText etEmail;
	private EditText etPassword;
	private EditText etPasswordVerify;
	private Button registerButton;
	private UtilityFunctions utilities;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		View view = inflater.inflate(R.layout.fragment_register, container, false);
		Typeface font = Typeface.createFromAsset( getActivity().getAssets(), "fontawesome-webfont.ttf" );
		((Button)view.findViewById(R.id.btnSubmitRegistration)).setTypeface(font);
		
		utilities = new UtilityFunctions();
		etName = (EditText) view.findViewById(R.id.etFullName);
		etEmail = (EditText) view.findViewById(R.id.etEmail);
		etPassword = (EditText) view.findViewById(R.id.etPasswordRegister);
		etPasswordVerify = (EditText) view.findViewById(R.id.etPasswordVerify);
		registerButton = (Button) view.findViewById(R.id.btnSubmitRegistration);
		registerButton.setOnClickListener(this);
		
		return view;
	}
	
	@Override
	public void onClick(View v) {
		attemptRegistration();
		
	}
	
	public void attemptRegistration() {
		boolean validationError = false;
		StringBuilder validationErrorMessage = new StringBuilder(getResources()
				.getString(R.string.error_intro));

		if (utilities.isEmpty(etName)) {
			validationError = true;
			validationErrorMessage.append(getResources().getString(
					R.string.error_blank_name));
		}

		if (utilities.isEmpty(etPassword)) {
			if (validationError == true) {
				validationErrorMessage.append(getResources().getString(
						R.string.error_join));
			}
			validationError = true;
			validationErrorMessage.append(getResources().getString(
					R.string.error_blank_password));
		}

		if (utilities.isMatching(etPassword, etPasswordVerify) == false) {
			if (validationError == true) {
				validationErrorMessage.append(getResources().getString(
						R.string.error_join));
			}

			validationError = true;
			validationErrorMessage.append(getResources().getString(
					R.string.error_mismatched_passwords));
		}

		if (utilities.isEmpty(etPasswordVerify)) {
			if (validationError == true) {
				validationErrorMessage.append(getResources().getString(
						R.string.error_join));
			}

			validationError = true;
			validationErrorMessage.append(getResources().getString(
					R.string.error_blank_password));
		}

		if (utilities.isEmpty(etEmail)) {
			if (validationError == true) {
				validationErrorMessage.append(getResources().getString(
						R.string.error_join));
			}

			validationError = true;
			validationErrorMessage.append(getResources().getString(
					R.string.error_invalid_email));
		}

		validationErrorMessage.append(getResources().getString(
				R.string.error_end));

		// If there is a validation error, display the error
		if (validationError == true) {
			Toast.makeText(getActivity(), validationErrorMessage.toString(),
					Toast.LENGTH_LONG).show();
			return;
		}

		// Set up the progress dialog
		final ProgressDialog progressDialog = new ProgressDialog(getActivity());
		progressDialog.setTitle("Please wait...");
		progressDialog.setMessage("Signing you up for YouPark. Please wait...");
		progressDialog.show();

		// setting up a ParseUser
		ParseUser user = new ParseUser();
		user.setUsername(etEmail.getText().toString());
		user.setEmail(etEmail.getText().toString());
		user.setPassword(etPassword.getText().toString());
		user.put("fullName", etName.getText().toString());
		user.signUpInBackground(new SignUpCallback() {

			@Override
			public void done(ParseException e) {
				progressDialog.dismiss();

				if (e != null) {
					Log.e("Parse User Sign Up", e.getMessage().toString());
					Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_LONG).show();
				} else {
					dispatch();
				}
			}
		});
	}

	private void dispatch() {
		if (ParseUser.getCurrentUser() != null) {
			Log.d("ParseUser", ParseUser.getCurrentUser().getEmail());
			startActivity(new Intent(getActivity(), MyMap.class));
			getActivity().finish();
		} else {
			startActivity(new Intent(getActivity(), MainActivity.class));
			getActivity().finish();
		}
	}
}
